package com.LeeCode;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * 全排列
 */

public class Code046 {
    public static void main(String[] args) {
        int[] nums = {1, 2, 3};
        System.out.println(new Code046().permute(nums));
    }

    public List<List<Integer>> permute(int[] nums) {
        List<List<Integer>> res = new ArrayList<>();
        int n = nums.length - 1;
        List<Integer> output = new ArrayList<>();
        for (int num : nums) {
            output.add(num);
        }
        findPermute(n, res, output, 0);
        return res;
    }

    public void findPermute(int n, List<List<Integer>> res, List<Integer> output, int index) {
        if (index == n) {
            res.add(new ArrayList<>(output));
        }
        for (int i = index; i <= n; i++) {
            Collections.swap(output, index, i);
            findPermute(n, res, output, index + 1);
            Collections.swap(output, index, i);
        }
    }

}
